package ljt.music.module.tab4;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import ljt.music.R;
import ljt.music.module.base.BaseFragment;
import ljt.music.ui.playlist.PlayListActivity;

/**
 * 视频页面
 *
 * @author jiangtao
 * @date 2017/11/25
 */
public class Tab4Fragment extends BaseFragment implements View.OnClickListener {
  private static Tab4Fragment instance;
  private EditText name;
  private Button sreach;

  public static Tab4Fragment getInstance() {
    if (instance == null) {
      instance = new Tab4Fragment();
    }
    return instance;
  }

  @Override
  protected int attachLayoutId() {
    return R.layout.fragment_tab4;
  }

  @Override
  protected void initView(View view) {
    name = view.findViewById(R.id.name);
    sreach = view.findViewById(R.id.sreach);

    sreach.setOnClickListener(this);
  }

  @Override
  protected void initData() {

  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
      case R.id.sreach:
        dialogList();
        break;
      default:
    }
  }

  private void dialogList() {
    final String items[] = {"网易", "腾讯", "虾米", "酷狗", "百度"};

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("列表");
    // builder.setMessage("是否确认退出?"); //设置内容
    builder.setIcon(R.mipmap.ic_launcher);
    // 设置列表显示，注意设置了列表显示就不要设置builder.setMessage()了，否则列表不起作用。
    builder.setItems(items, new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        Toast.makeText(getActivity(), items[which],
            Toast.LENGTH_SHORT).show();

        String trim = name.getText().toString().trim();
        Intent intent = new Intent(getActivity(), PlayListActivity.class);
        intent.putExtra("types", "search");
        intent.putExtra("count", "100");
        intent.putExtra("pages", "1");
        if (trim != null && !trim.equals("")) {
          intent.putExtra("name", trim);
        }

        if (items[which].equals("网易")) {
          intent.putExtra("source", "netease");
        } else if (items[which].equals("腾讯")) {
          intent.putExtra("source", "tencent");
        } else if (items[which].equals("虾米")) {
          intent.putExtra("source", "xiami");
        } else if (items[which].equals("酷狗")) {
          intent.putExtra("source", "kugou");
        } else if (items[which].equals("百度")) {
          intent.putExtra("source", "baidu");
        }
        startActivity(intent);
      }
    });
    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {
        dialog.dismiss();
        Toast.makeText(getActivity(), "确定", Toast.LENGTH_SHORT)
            .show();
      }
    });
    builder.create().show();
  }
}
